package com.hinnotek.amoeba.ui.activity.old

import android.os.Bundle
import android.text.TextUtils
import androidx.activity.viewModels
import com.hinnotek.amoeba.R
import com.lee.common.base.BaseActivity
import com.hinnotek.amoeba.databinding.ActivityChangeLoginPasswordBinding
import com.hinnotek.amoeba.bean.old.UserChangeLoginPwdBean
import com.lee.common.utils.SharedPreUtils
import com.hinnotek.amoeba.viewmodels.UserViewModel
import com.lee.common.network.BaseResp
import com.lee.common.network.net.ObserverState
import com.xuexiang.xui.utils.WidgetUtils
import com.xuexiang.xui.widget.dialog.MiniLoadingDialog
import com.xuexiang.xui.widget.toast.XToast
import dagger.hilt.android.AndroidEntryPoint

/**
 *用户修改登录密码
 */
@AndroidEntryPoint
class ChangeLoginPasswordActivity : BaseActivity<ActivityChangeLoginPasswordBinding>() {

    private lateinit var mMiniLoadingDialog: MiniLoadingDialog
    private var userId = 0
    private val viewModel by viewModels<UserViewModel>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_change_login_password)
        setTitle("修改密码")

    }

    override fun initVew() {

        userId = SharedPreUtils.getInteger("userId", 0)
        mMiniLoadingDialog = WidgetUtils.getMiniLoadingDialog(mContext, "提交中......")

        bindingView.buttonChangeLoginPWDBtn.setOnClickListener {
            if (TextUtils.isEmpty(bindingView.etOldPwdET.getText().toString().trim())) {
                XToast.error(mContext, "原密码不能为空！").show()
                bindingView.etOldPwdET.requestFocus()
                return@setOnClickListener
            }
            if (TextUtils.isEmpty(bindingView.etNewPwdET.getText().toString().trim())) {
                XToast.error(mContext, "新密码不能为空！").show()
                bindingView.etNewPwdET.requestFocus()
                return@setOnClickListener
            }
            if (bindingView.etNewPwdET.text.toString()
                    .trim() != bindingView.etNewPwdET2.text.toString().trim()
            ) {
                XToast.error(mContext, "两次密码输入不一致！").show()
                bindingView.etNewPwdET2.requestFocus()
                return@setOnClickListener
            }
            userChangePwdNet()
        }
    }

    override fun setData() {}

    /**
     * 用户修改密码
     */
    private fun userChangePwdNet() {
        val oldPasswordStr = bindingView.etOldPwdET.text.toString().trim()
        val newPasswordStr = bindingView.etNewPwdET.text.toString().trim()
        mMiniLoadingDialog.show()

        viewModel.changePass(userId, oldPasswordStr, newPasswordStr)
            .observe(this, object : ObserverState<UserChangeLoginPwdBean>() {
                override fun onChanged(t: BaseResp<UserChangeLoginPwdBean>) {
                    mMiniLoadingDialog.dismiss()
                    XToast.success(mContext, "修改成功!").show()
                    super.onChanged(t)
                }
                override fun onError(message: String?, code: Int) {
                    mMiniLoadingDialog.dismiss()
                    XToast.error(mContext, message.toString()).show()
                    super.onError(message, code)
                }
            })
    }
}